Quick Start PRO
VideoRecorder is a high-level video capture and recording module provided by Scripting.
It encapsulates complex AVFoundation details such as AVCaptureSession management, audio/video synchronization, orientation handling, encoding, and pause/resume timelines, and exposes a state-driven, script-friendly API.
Typical use cases include:
- Video recording with pause and resume
- Synchronized audio capture
- High frame rate and high bitrate recording
- ProRes video encoding
- Capturing photos during video recording
- Runtime camera control (focus, exposure, zoom, torch)
- UI-agnostic preview rendering via a separate preview view
Design Principles
State-Driven Architecture
VideoRecorder is governed by a strict internal state machine.
All public APIs are validated against the current state to prevent invalid transitions and undefined behavior.
State definitions:
-
idle Initial state or fully reset. No active capture session.
-
preparing Capture session, devices, and encoders are being configured.
-
ready Fully prepared and ready to start recording.
-
recording Actively recording video (and audio if enabled).
-
paused Recording timeline is paused; media writing is suspended.
-
stopping Recording is ending and the output file is being finalized.
-
finished Recording completed successfully.
detailscontains the output file path. -
failed An error occurred.
detailscontains the error message.
Capture Session
VideoRecorder.session exposes a read-only AVCaptureSession instance managed internally by VideoRecorder.
This session is intended for:
- Attaching preview views
- Integration with other components that require direct access to the capture session
The session cannot be instantiated or modified directly.
Recorder Configuration
Camera Selection
-
positionSelects the front or back camera. -
preferredTypesA prioritized list of physical camera types, such as:"wide""ultraWide""telephoto""triple"
If not specified, the system automatically selects an appropriate camera for the chosen position.
Frame Rate
Supported frame rates:
- 24
- 30 (default)
- 60
- 120 (device-dependent)
Audio Recording
Enables or disables audio recording.
Defaults to true.
Session Preset
Controls capture resolution and quality, for example:
"high""hd1920x1080""hd4K3840x2160"
Video Codec
Supported codecs include:
"hevc"(default)"h264""hevcWithAlpha""proRes422""proRes4444""appleProRes4444XQ""proResRAW"
Availability depends on device capabilities and OS version.
Video Bit Rate
Specifies the target video bitrate in bits per second.
Default value is 5_000_000.
Only applies to certain codecs.
Orientation
Defines the recording orientation and affects both pixel buffers and output metadata.
Front Camera Mirroring
Mirrors the front camera image if set to true.
Defaults to false.
Audio Session Management
-
true(default) The system automatically configures the sharedAVAudioSessionfor optimal recording. The original audio session state is not restored after recording. -
falseThe app is responsible for configuringAVAudioSession. Incompatible settings may cause recording to fail.
State Access and Observation
Get Current State
Returns the current state of the recorder.
State Change Listener
-
stateThe new recorder state. -
details- For
"failed": error description - For
"finished": output file path
- For
If no listener is provided, all listeners are removed.
Recording Lifecycle
Prepare
- Creates and configures the capture session
- Requests camera and microphone permissions
- Initializes encoders
Transitions to the ready state upon success.
Start Recording
- Begins recording
- Writes output to the specified file path
- Transitions to
recording
Pause and Resume
- Pauses and resumes the recording timeline
- Does not create separate files
- Suitable for long-form or segmented recording
Stop Recording
- Finalizes the recording
- Transitions to
finished detailscontains the output file path
Cancel Recording
- Aborts recording
- Deletes the output file
- Does not enter the
finishedstate
Reset Recorder
- Closes the capture session
- Clears all internal state
- Returns to the
idlestate
Typically used when switching cameras or fully releasing resources.
Photo Capture During Recording
- Only valid while in the
recordingstate - Captures a still image from the current video stream
- Does not interrupt recording
Camera Controls
Torch (Flashlight)
Focus and Exposure
- Coordinates are normalized (0.0–1.0)
{ x: 0, y: 0 }corresponds to the top-left corner
Zoom Control
On iOS 18 and later:
These values are intended for user-friendly zoom display in the UI.
Typical Usage Flow
Usage Notes and Best Practices
- A full lifecycle follows:
prepare → start → stop | cancel - Switching cameras during
recordingis not recommended; callresetfirst - High frame rates and ProRes codecs require significant performance and storage
- When disabling automatic audio session configuration, ensure compatibility manually
- Preview rendering is decoupled from recording logic and should be handled separately
